{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "535215cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d029e8bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引列\n",
      "0    9.8\n",
      "1    7.0\n",
      "2    6.0\n",
      "Name: Series对象, dtype: float64\n",
      "a    9\n",
      "b    8\n",
      "c    7\n",
      "d    6\n",
      "dtype: int64\n",
      "Index(['a', 'b', 'c', 'd'], dtype='object')\n",
      "a    9\n",
      "b    8\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#Series类型，由一组数据及这相关的数据索引组成。\n",
    "#包含index与array\n",
    "a = pd.Series([9.8,7,6])\n",
    "a.name = 'Series对象'\n",
    "a.index.name='索引列'\n",
    "\n",
    "print(a)\n",
    "b = pd.Series([9,8,7,6],index=['a','b','c','d'])\n",
    "print(b)\n",
    "\n",
    "print(b.index)\n",
    "b.values\n",
    "print(b[b>7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f10f97d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   two three\n",
       "b    8   NaN\n",
       "c    7   NaN\n",
       "d    6   NaN"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame 由共用相同索引的一组列组成\n",
    "dt = {'one':pd.Series([1,2,3],index=['a','b','c']),\n",
    "      'two':pd.Series([9,8,7,6],index=['a','b','c','d'])}\n",
    "a = pd.DataFrame(dt)\n",
    "pd.DataFrame(dt,index=['b','c','d'],columns=['two','three'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cd20730",
   "metadata": {},
   "source": [
    "## 报错\n",
    "1. Length of values (3) does not match length of index (4)\n",
    "    - 结果是：'one':pd.Series([1,2,3],index=['a','b','c','d']，其中的[1,2,3]与['a','b'..]不匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d6d7b5d4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['c1', 'c2', 'c3', 'c4', 'c5', 'c6'], dtype='object')\n",
      "Index(['城市', '环比', '同比', '定基'], dtype='object')\n",
      "[['北京' 101.5 120.7 128.7]\n",
      " ['上海' 101.2 127.3 159.3]\n",
      " ['广州' 101.3 119.4 124.4]\n",
      " ['深圳' 102.0 140.9 456.9]\n",
      " ['沈阳' 100.1 101.4 231.4]\n",
      " ['福建' 102.4 123.5 125.5]]\n",
      "城市       上海\n",
      "环比    101.2\n",
      "同比    127.3\n",
      "定基    159.3\n",
      "Name: c2, dtype: object\n"
     ]
    }
   ],
   "source": [
    "dl ={'城市':['北京','上海','广州','深圳','沈阳','福建'],\n",
    "    '环比':[101.5,101.2,101.3,102.0,100.1,102.4],\n",
    "    '同比':[120.7,127.3,119.4,140.9,101.4,123.5],\n",
    "    '定基':[128.7,159.3,124.4,456.9,231.4,125.5]}\n",
    "d = pd.DataFrame(dl,index=['c1','c2','c3','c4','c5','c6'])\n",
    "print(d.index,d.columns,d.values,sep='\\n')\n",
    "print(d.loc['c2'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41b42426",
   "metadata": {},
   "source": [
    "## 算数运算法则\n",
    "- 行列\n",
    "- 广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6028e31a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3   4\n",
      "0   0   1   2   3   4\n",
      "1   5   6   7   8   9\n",
      "2  10  11  12  13  14\n",
      "3  15  16  17  18  19\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "dtype: int32\n",
      "count    4.000000\n",
      "mean     1.500000\n",
      "std      1.290994\n",
      "min      0.000000\n",
      "25%      0.750000\n",
      "50%      1.500000\n",
      "75%      2.250000\n",
      "max      3.000000\n",
      "dtype: float64 <class 'pandas.core.series.Series'>\n",
      "               0          1          2          3          4\n",
      "count   4.000000   4.000000   4.000000   4.000000   4.000000\n",
      "mean    7.500000   8.500000   9.500000  10.500000  11.500000\n",
      "std     6.454972   6.454972   6.454972   6.454972   6.454972\n",
      "min     0.000000   1.000000   2.000000   3.000000   4.000000\n",
      "25%     3.750000   4.750000   5.750000   6.750000   7.750000\n",
      "50%     7.500000   8.500000   9.500000  10.500000  11.500000\n",
      "75%    11.250000  12.250000  13.250000  14.250000  15.250000\n",
      "max    15.000000  16.000000  17.000000  18.000000  19.000000 <class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "# 广播\n",
    "#Series 是索引+一维，DataFrame是索引加多维，将它们看成是一个单一数据去处理即可。\n",
    "\n",
    "b = pd.DataFrame(np.arange(20).reshape(4,5))\n",
    "c = pd.Series(np.arange(4))\n",
    "print(b,c,sep='\\n')\n",
    "b-c #可以看到第0行的0-4与 c的0-4相互运算\n",
    "c>0 #比较运算\n",
    "# b.sort_index(ascending=False) #降序排序 ascending\n",
    "print(c.describe(),type(c.describe()))\n",
    "print(b.describe(),type(b.describe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137b8e3e",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
